import requests from './request'

export const login = (username, password) => requests({
    url: '/login',
    method: 'post',
    data: {username, password}
})
export const checkNameIfExist = (username_string) => requests({
    url: '/checkUserExist',
    method: 'get',
    params: {username: username_string}
})

// 优化：支持 FormData 直接传递
export const register = (formDataOrUsername, name, password, avatar) => {
    if (formDataOrUsername instanceof FormData) {
        return requests({
            url: '/reg',
            method: 'POST',
            data: formDataOrUsername,
            headers: {'Content-Type': 'multipart/form-data'}
        })
    } else {
        return requests({
            url: '/reg',
            method: 'POST',
            data: {username: formDataOrUsername, name, password, avatar: avatar ?? null},
            headers: {'Content-Type': 'multipart/form-data'}
        })
    }
}
